package com.tencent.qcloud.tuikit.tuichat.ui.view.message.viewholder;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.Group;

import com.bumptech.glide.Glide;
import com.cqyc.network.getid.GetMyInformationBox;
import com.cqyc.network.http.NConfig;
import com.cqyc.network.utils.Utils;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.bean.message.CustomGoodsMessageBean;
import com.tencent.qcloud.tuikit.tuichat.bean.message.CustomMyOrderMessageBean;
import com.tencent.qcloud.tuikit.tuichat.bean.message.TUIMessageBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class CustomMyOrderMessageHolder extends MessageContentHolder {
    private Group group;
    private TextView number;
    private TextView time;
    private ImageView imageView;
    private TextView title;
    private TextView price;

    public CustomMyOrderMessageHolder(View itemView) {
        super(itemView);
        group = itemView.findViewById(R.id.top_group);
        number = itemView.findViewById(R.id.order_number);
        time = itemView.findViewById(R.id.order_time);
        imageView = itemView.findViewById(R.id.order_image);
        title = itemView.findViewById(R.id.order_title);
        price = itemView.findViewById(R.id.order_price);
    }

    public static final String TAG = CustomMyOrderMessageHolder.class.getSimpleName();

    @Override
    public int getVariableLayout() {
        return R.layout.custom_goods_message_layout;
    }

    @Override
    public void layoutVariableViews(TUIMessageBean msg, int position) {
        msgArea.setPadding(0, 0, 0, 0);
        msgArea.setBackgroundResource(R.color.transparent);

        CustomMyOrderMessageBean messageBean = null;
        if (msg instanceof CustomMyOrderMessageBean) {
            messageBean = (CustomMyOrderMessageBean) msg;
        }
        if (messageBean == null) {
            return;
        }
        group.setVisibility(View.VISIBLE);
        number.setText("订单号：" + messageBean.getDataModel().getTrade_sn());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date;
        try {
            date = simpleDateFormat.parse(messageBean.getDataModel().getCreated_at());
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String newTime = simpleDateFormat.format(date);
            time.setText(newTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Glide.with(imageView).load(messageBean.getDataModel().getTrade_items().get(0).getSku_img()).into(imageView);
        title.setText(messageBean.getDataModel().getTrade_items().get(0).getSku_name());
        price.setText(String.valueOf(messageBean.getDataModel().getPayment()));

        msgContentFrame.setClickable(true);
        String finalLink = messageBean.getDataModel().getTrade_web_url();
        msgContentFrame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (GetMyInformationBox.INSTANCE.isBuyer()) {
                    Bundle bundle = new Bundle();
                    Map<String, String> map = new HashMap<>();
                    map.put("token", GetMyInformationBox.INSTANCE.getToken());
                    map.put("app_id", GetMyInformationBox.INSTANCE.getAppId());
                    map.put("android_version_code", GetMyInformationBox.INSTANCE.getVersion());
                    map.put("platform", "2");

                    String path = Utils.Companion.buildUrl(finalLink, map);
                    bundle.putString(NConfig.Companion.getWebViewUrl(), path);
                    bundle.putBoolean(NConfig.Companion.getWebViewTitleVisible(), false);
                    TUICore.startActivity(NConfig.Companion.getWebName(), bundle);
                }
            }
        });
    }
}
